﻿
/* 

MAD.js is an in-house e-commerce javascript library
copyright 2009,2010 MAD Productions

version 1.3


MAD.js是一个内部的电子商务JavaScript库
版权2009,2010疯狂制作

版本1.3
*/
var MAD = {};
MAD.core = (function() {
    //做一个小的浏览器测试

    //  var _browser = {
    //		ie:(window.ActiveXObject) ? true : false
    //	};
    //指定卡图像路径
    var _cardImgs = {
        delta: "url(/common/images/checkout/delta.gif) no-repeat",
        electron: "url(/common/images/checkout/electron.gif) no-repeat",
        maestro: "url(/common/images/checkout/maestro.gif) no-repeat",
        mastercard: "url(/common/images/checkout/mastercard.gif) no-repeat",
        solo: "url(/common/images/checkout/solo.gif) no-repeat",
        visa: "url(/common/images/checkout/visa.gif) no-repeat",
        amex: "url(/common/images/checkout/amex.gif) no-repeat"
    };
    //函数来处理信用卡结账的卡标志chaning
    var _changeCard = function(e, args) {
        //从配置obj的ID选择框EL
        var selectEl = (args.id != null) ? document.getElementById(args.id) : null;
        //得到跨度的ID，持有该卡的形象
        var imgEl = (args.imgId != null) ? document.getElementById(args.imgId) : null;
        //获取选定的卡（通过IE的篮球跳跃）
        var cardIndex = selectEl.selectedIndex;
        //删除图像，如果选择第一个选项
        var cardChoice = selectEl[cardIndex].text.toLowerCase(); (cardIndex == 0) ? imgEl.style.background = "none" : null;
        //得到形象的路径
        var imgPath = _cardImgs[cardChoice];
        //设置跨度BG形象
        imgEl.style.background = imgPath
    };
    //书签
    var _bookmark = function(title, url) {
        if (window.sidebar) {
            window.sidebar.addPanel(title, url, "")
        } else {
            if (document.all) {
                window.external.AddFavorite(url, title)
            } else {
                if (window.opera && window.print) {
                    return true
                }
            }
        }
    };
    //修正后回页开/常闭地址框
    var _closeAddress = function() {
        $(this).parent().find(".genericAddress").slideUp(700);
        $(".slideParentSame").children("div").addClass("existingOn").removeClass("existingOff");
        $(".slideParentDiff").children("div").addClass("existingOff").removeClass("existingOn")
    };
    var _openAddress = function() {
        $(this).parent().find(".genericAddress").slideDown(700);
        $(".slideParentSame").children("div").addClass("existingOff").removeClass("existingOn");
        $(".slideParentDiff").children("div").addClass("existingOn").removeClass("existingOff")
    };
    return {
        changeCard: _changeCard,
        bookmark: _bookmark
    }
})();
/*
灯箱
*/
MAD.lightBox = (function() {
    var _init = function() {
        $(".overlay").click(function() {
            if (!$(".modal").is(":visible")) {
                $(".lightbox").hide();
                $(".expandedContent").hide();
                $(this).hide()
            }
        });
        $(".lightbox .btn_close").attr("href", "javascript:MAD.lightBox.hide();")
    };
    var _show = function(id) {
        $(id).css("top", ($(window).height() - $(id).height()) / 2 + $(window).scrollTop() + "px");
        $(id).css("left", ($(window).width() - $(id).width()) / 2 + $(window).scrollLeft() + "px");
        $(".overlay").show();
        $(id).show();
        return false
    };
    var _hide = function() {
        $(".lightbox").hide();
        $(".overlay").hide()
    };
    return {
        init: _init,
        show: _show,
        hide: _hide
    }
})();
//产品相关的功能
//鼠标移动到产品上面的显示产品信息
MAD.products = (function() {
    var _Products = [];
    //添加产品，以收集
    var _addProduct = function(o) {
        _Products[o.id] = o
    };
    //查找数组中的产品
    var _findProduct = function(i) {
        return _Products[i]
    };
    //大产品图片灯箱 - （产品对象）
    function enlargeProduct(i) {
        var enProd = i;
        $(".lbEnlargeProduct").find("img").attr("src", "");
        $(".lbEnlargeProduct").find("h3").html(enProd.brandName + " " + enProd.name);
        $(".lbEnlargeProduct").find(".commMsg").html(enProd.commercialMsg);
        $(".lbEnlargeProduct").find("img").attr("src", enProd.image);
        $(".lbEnlargeProduct").find("a.btn_viewProdDetails").attr("href", enProd.href);
        $(".lbEnlargeProduct").click(function() {
            MAD.lightBox.hide()
        });
        showLightBox(".lbEnlargeProduct")
    }
    //显示ID我的产品，扩大在指定容器
    var _showProduct = function(target, sender, i) {
        var link = $(sender).find("h3 a").attr("href");
        target = "#" + target;
        var aTitle = $(target + "_aTitle");
        aTitle.attr("href", link);
        $(target + "_aView").attr("href", link);
        $(target + "_imgProduct").parents("a").attr("href", link);
        var img = $(target + "_imgProduct");
        img.attr("src", $(sender).find("img").attr("src"));
        img.attr("width", $(sender).find("img").attr("width"));
        img.attr("height", $(sender).find("img").attr("height"));
        var o = _findProduct(i);
        //放大悬停弹出窗口形象
        $(".magnify").click(function() {
            enlargeProduct(o);
            return false
        });
        aTitle.html("<strong>" + o.brandName + "</strong> " + o.name);
        aTitle.title = o.brandName + " " + o.name;
        aTitle.html($(sender).find("h3 a").html()); //获取名称
        aTitle.title = $(sender).find("h3 a").text(); ////获取名称
        if ($(sender).find(".image span").attr("class")) {
            $(target + "_Sash").attr("class", $(sender).find(".image span").attr("class"))
        } else {
            $(target + "_Sash").attr("class", "")
        }
        if ($(sender).find(".image span").css("background-image")) {
            $(target + "_Sash").css("background-image", $(sender).find(".image span").css("background-image"))
        } else {
            $(target + "_Sash").css("background-image", "")
        }
        $(target).find(".specialTag").remove();
        $(target).find(".newTag").remove();
        if ($(sender).find(".specialTag").attr("class")) {
            $(target).append('<div class="specialTag"></div>')
        }
        if ($(sender).find(".newTag").attr("class")) {
            $(target).append('<div class="newTag"></div>')
        }
        $(target + "_CommMsg").html(o.commercialMsg);
        $(target + "_CommMsg").html(o.commercialMsg); //不知道怎么改
        if (o.price == "HIDE") {
            $(target + "_Price").html('<a href="' + o.href + '" title="Click for price">Click for price</a>');
            $(target + "_Price").html('<a href="' + $(sender).find("price a").attr("href") + '" title="Click for price">Click for price</a>');
            $(target + "_Save").html("");
            $(target + "_RRP").html("")
        } else {
            if (o.price == "titleist") {
                $(target + "_Price").html('<a href="#"><strong></strong></a>');
                $(target + "_Save").html("");
                $(target + "_RRP").html("")
            } else {
                $(target + "_Price").html("PRICE <strong>$" + o.price + "</strong>");
                if (o.save > 0) {
                    $(target + "_Save").html("SAVE <span>" + o.save + "%</span>");
                    $(target + "_RRP").html("MRRP $" + o.rrp)
                } else {
                    $(target + "_Save").html("");
                    $(target + "_RRP").html("")
                }
            }
        }
        var divRating = $(target + "_Rating");
        if (o.rating > 0) {
            divRating.removeClass("norating");
            divRating.removeClass("rating");
            divRating.removeClass("stockWarning");
            divRating.addClass("rating");
            divRating.html('<div class="count" style="width:' + o.rating + 'px;"></div>')
        } else {
            divRating.removeClass("norating");
            divRating.removeClass("rating");
            divRating.removeClass("stockWarning");
            divRating.addClass("norating");
            divRating.html("<!-- -->")
        }
    };
    //产品悬停功能
    var slightDelay;
    var dynamicClass;
    var objTopPad;
    var top;
    var left;
    //鼠标移动到上面显示层函数
    var _attachProductHover = function() {
        $(".withHover").hoverIntent({
            sensitivity: 3, //数=灵敏度阈值（必须是1或更高）
            interval: 200, //的onmouseover轮询间隔=毫秒
            timeout: 0, //数毫秒的延迟之前onMouseOut 
            over: function() {
                $(".productPopupContainer").slideDown("fast", 0)
                dynamicClass = ($(this).parents("div").attr("class") + "Hover");
                var pos = $(this).offset();
                left = pos.left;
                top = pos.top;
                objTopPad = parseInt($(this).css("padding-top"));
                left -= 18;
                top -= (38 - objTopPad);
                if ($(this).hasClass("divide") && $(this).parents("div").hasClass("subCatGrid")) {
                    left = left + 11
                }
                if ($(this).hasClass("divide") && $(this).parents("div").hasClass("subCatGrid") && $(this).parents("div").hasClass("nike")) {
                    left = left - 11
                }
                if ($(this).hasClass("divide") && $(this).parents("div").hasClass("subCatGrid") && $(this).parents("div").hasClass("taylormade")) {
                    left = left - 10
                }
                if ($(this).hasClass("divide") && $(this).parents("div").hasClass("subCatGrid") && $(this).parents("div").hasClass("cobra")) {
                    left = left - 11
                }
                $(".productPopupContainer").css({
                    left: left,
                    top: top
                });
                var proddiv = $(this);
                MAD.products.showProduct(enlargeContID, proddiv, proddiv.attr("prod"));
                $(".productPopupContainer").mouseover()
            },
            out: function() { }
        })
    }; //鼠标移开执行函数
    var _productPopupHover = function() {
        $(".productPopupContainer").hover(function(event) {
            $(".productPopup").addClass(dynamicClass);
            $(".productPopupContainer").show()
            // $(".productPopupContainer").slideDown("fast")
        },
        function() {
            $(".productPopup").removeClass(dynamicClass);
            $(".productPopupContainer").hide()
            //$(".productPopupContainer").slideUp("fast")
        })
    };
    //返回的公共方法
    return {
        addProduct: _addProduct,
        findProduct: _findProduct,
        Products: _Products,
        showProduct: _showProduct,
        attachProductHover: _attachProductHover,
        productPopupHover: _productPopupHover
    }
})();
/*命名空间的虚拟对象*/
MAD.entities = {};
/*产品类*/
/*function（ID，名称，品牌，HREF，图片，CommercialMsg，促销，原价，节省，评价，缺货）*/
MAD.entities.product = function(ID, Name, BrandName, Href, Image, CommercialMsg, Price, RRP, Save, Rating, OOS) {
    this.id = ID;
    this.name = Name;
    this.brandName = BrandName;
    this.href = Href;
    this.image = Image;
    this.commercialMsg = CommercialMsg;
    this.price = Price;
    this.rrp = RRP;
    this.save = Save;
    this.rating = Rating;
    this.oos = OOS;
};
/**/
MAD.pages = {};
/*页面初始化函数*/
MAD.pages.all = (function() {
    /*向集合中添加产品*/
    var _init = function() {
        _flyouts();
        _newsletter();
        _toolbar();
        _searchDropDown();
        _navDelay();
        _carouselTips()
    };
    //偏好英美弹出按钮
    var _flyouts = function() {
        $("#header ul.preferences li a.expand").click(function(e) {
            e.preventDefault();
            var parentObj = $(this).parent();
            parentObj.find(".ieShim").slideToggle(); //IE6的IFRAME SHIM修复
            if (parentObj.find(".expandedContent").is(":hidden")) {
                parentObj.find("a.expand").addClass("open");
                parentObj.find(".expandedContent").stop(true, true).slideDown("slow")
            } else {
                //调用回调标签流畅的动画效果
                parentObj.find(".expandedContent").stop(true, true).slideUp("slow",
                function() {
                    parentObj.find("a.expand").removeClass("open")
                })
            }
            $("ul.preferences li a.expand").not(this).removeClass("open");
            $("ul.preferences li a.expand").not(".open").parent().find(".expandedContent").hide();
            $("ul.preferences li").not(parentObj).find(".ieShim").hide();
            return false
        });
        //弹出关闭
        $("#header ul.preferences li .expandedContent a.closeBox").click(function() {
            var closeObj = $(this).parent();
            closeObj.parent().find(".ieShim").hide();
            closeObj.slideUp("slow",
            function() {
                closeObj.parent().find("a.expand").removeClass("open")
            });
            return false
        })
    };
    //注册电子邮件通讯
    var _newsletter = function() {
        $(".newsletterEmail").focus(function() {
            $(this).val("")
        });
        $(".newsletterEmail").blur(function() {
            if ($(this).attr("value") == "") {
                $(this).val("--Enter Email Address--")
            }
        })
    };
    //浮动工具栏
    var _toolbar = function() {
        if ($.cookie("ftbState") == "closed") {
            $(".toolbarContainer ul.tbSlide").hide();
            $(".toolbar .tbToggle").addClass("closed")
        }
        $(".toolbarContainer").show();
        $(".toolbar a.expBtn").click(function() {
            var self = $(this).parent().find(".expWrap");
            $(".toolbar .expWrap").not(self).slideUp();
            $(this).parent().find(".expWrap").stop(true, true).slideToggle();
            if ($(this).hasClass("facebook")) {
                var $fb = $('<iframe src="http://www.facebook.com/plugins/likebox.php?id=18727097221&amp;width=402&amp;connections=7&amp;stream=true&amp;header=false&amp;height=480" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:402px; height:480px;" allowTransparency="true"></iframe>');
                $(this).parent().find(".inner .content").html($fb)
            }
            return false
        });
        $(".toolbar .tbToggle").click(function() {
            $(".toolbar ul").animate({
                width: "toggle"
            });
            $(this).toggleClass("closed");
            if ($(this).hasClass("closed")) {
                $.cookie("ftbState", "closed")
            } else {
                $.cookie("ftbState", null)
            }
            return false
        });
        $(".toolbar .expWrap .close").click(function() {
            $(this).parents(".expWrap").slideToggle();
            return false
        });
        if (navigator.platform == "iPad" || navigator.platform == "iPhone" || navigator.platform == "iPod") {
            $(".toolbarContainer").css("position", "static")
        }
    };
    //搜索下拉悬停
    var _searchDropDown = function() {
        $("#searchDropDown .product").live("mouseover mouseout",
        function() {
            $(this).toggleClass("hovered")
        })
    };
    //首页产品左右滚动
    var _impulseCarousel = function() {
        for (i = 1; i <= 2; i++) {
            if ($(".ImpCarousel" + i).length) {
                $(".ImpCarousel" + i + " .impulseCarousel").jCarouselLite({
                    btnNext: ".ImpCarousel" + i + " #next",
                    btnPrev: ".ImpCarousel" + i + " #prev",
                    visible: 1,
                    circular: false
                })
            }
        }
        if ($(".ImpCarousel1").length) {
            $(".ImpCarousel2").hide()
        }
    };
    var _navDelay = function() {
        $(".topLevel > li").hoverIntent({
            sensitivity: 3,
            interval: 200,
            timeout: 0,
            over: function() {
                $(this).addClass("open")
            },
            out: function() {
                $(this).removeClass("open")
            }
        })
    };
    var _carouselTips = function() {
        $(".control").hover(function() {
            if (!$(this).hasClass("disabled")) {
                $(this).find(".carouselTip").stop(false, true).fadeIn(300)
            }
        },
        function() {
            $(this).find(".carouselTip").stop(false, true).fadeOut(300)
        })
    };
    var _IE6 = function() {
        $(window).scroll(function() {
            $(".toolbarContainer").stop().animate({
                top: ($(window).height() + $(document).scrollTop() - 42)
            },
            0, "linear")
        })
    };
    //返回的公共方法
    return {
        init: _init,
        navDelay: _navDelay,
        impulseCarousel: _impulseCarousel,
        carouselTips: _carouselTips,
        IE6: _IE6
    }
})();
//这里有首页 的产品左右也的
/*
页面初始化函数
*/
MAD.pages.home = (function() {
    var _init = function() {
        _saleCarousel()
    };
    var _saleCarousel = function() {
        for (i = 1; i <= 5; i++) {
            if ($(".Carousel" + i).length > 0) {
                $(".Carousel" + i + " .saleItemsCarousel").jCarouselLite({
                    btnNext: ".Carousel" + i + " .next" + i,
                    btnPrev: ".Carousel" + i + " .prev" + i,
                    visible: 1,
                    scroll: 1,
                    circular: false//循环
                })
            }
        }
    };
    //返回的公共方法
    return {
        init: _init
    }
})();